package com.danskin.wechat.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.danskin.wechat.entity.StoreInfo;
import com.danskin.wechat.service.StoreInfoService;
import com.danskin.wechat.util.Page;

import java.io.BufferedReader;  
import java.io.IOException;  
import java.io.InputStreamReader;  
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.MalformedURLException;  
import java.net.URL;  
import java.net.URLConnection;  
import java.util.HashMap;
import java.util.List;
import java.util.Map;  
  
import net.sf.json.JSONObject;  
@Service
public class MapBaidu {
	
	
	private final static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;

	@Autowired
	private StoreInfoService storeInfoService;
	
	public static void main(String[] args) throws IOException {  
        MapBaidu getLatAndLngByBaidu = new MapBaidu();  
          
        Map<String,Double> map=getLatAndLngByBaidu.getLngAndLat("上海市闵行区陈行公路2688号");  
        System.out.println("经度："+map.get("lng")+"---纬度："+map.get("lat")); 
        
       Map<String, Double> gcj02 = Convert_BD09_To_GCJ02((Double)map.get("lat"),(Double)map.get("lng"));
       System.out.println("标准经度："+gcj02.get("lng")+"---标准纬度："+gcj02.get("lat")); 
    }  
	
	
	
	
	
	
	
	
	public static Map<String, Double> Convert_BD09_To_GCJ02(double lat, double lng) {
		Map<String,Double> map=new HashMap<>();
		double x = lng - 0.0065, y = lat - 0.006;
		double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
		double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
		lng = z * Math.cos(theta);
		lat = z * Math.sin(theta);
		map.put("lat", lat);
		map.put("lng", lng);
		return map;
	}
	
	
	
	
	public String setlngAndlat(Page page) throws Exception{
		MapBaidu getLatAndLngByBaidu = new MapBaidu();  
		
		List<StoreInfo> list = storeInfoService.listPageStrore(page);
		
		for(int i=0;i<list.size();i++){
			
			Map<String,Double> map=getLatAndLngByBaidu.getLngAndLat(list.get(i).getAddress());
			System.out.println("经度："+map.get("lng")+"---纬度："+map.get("lat")); 
			if(null!=map.get("lng")&&null!=map.get("lat")){
				list.get(i).setLatitude(BigDecimal.valueOf(map.get("lat")));
				list.get(i).setLongitude(BigDecimal.valueOf(map.get("lng")));
				storeInfoService.updateByPrimaryKeySelective(list.get(i));
				if(i%20==19){
					Thread.sleep(1000); 
				}
			}
			
		
			
		}
		
		
		
		
		return "a";
	}
	
	
	
	
	
	
      
    public static Map<String,Double> getLngAndLat(String address){  
        Map<String,Double> map=new HashMap<String, Double>();  
         String url = "http://api.map.baidu.com/geocoder/v2/?address="+address.trim()+"&output=json&ak=ffBCmoxkXPT1B0kBYmxNhkg0fQ7BlZQo";  
         
            String json = loadJSON(url);
            if(StringUtils.isNotBlank(json)){
            System.out.println(json);
            JSONObject obj = JSONObject.fromObject(json);  
            if(obj.get("status").toString().equals("0")){  
                double lng=obj.getJSONObject("result").getJSONObject("location").getDouble("lng");  
                double lat=obj.getJSONObject("result").getJSONObject("location").getDouble("lat");  
                map.put("lng", lng);  
                map.put("lat", lat);  
                
               // System.out.println("经度："+lng+"---纬度："+lat);  
            }else{  
                //System.out.println("未找到相匹配的经纬度！");  
            }
            }
        return map;  
    }  
      
     public static String loadJSON (String url) {  
            StringBuilder json = new StringBuilder();  
            try {  
                URL oracle = new URL(url);  
                URLConnection yc = oracle.openConnection();  
                BufferedReader in = new BufferedReader(new InputStreamReader(  
                                            yc.getInputStream()));  
                String inputLine = null;  
                while ( (inputLine = in.readLine()) != null) {  
                    json.append(inputLine);  
                }  
                in.close();  
            } catch (MalformedURLException e) {  
            } catch (IOException e) {  
            }  
            return json.toString();  
        }  
	
	
}
